using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Wax;
using NUnit.Framework;
using WatiN.Core;
using System;

namespace WaxLibTest
{
    /// <summary>
    /// Tests the behavior of the <see cref="CommandException"/> class.
    /// </summary>
    [TestFixture]
    public class CommandExceptionTest
    {
        [Test]
        public void Serialize()
        {
            Command command = new Command();
            command.Arguments.Add("arg1");
            command.Arguments.Add("arg2");
            command.Id = "cmdTest1";
            command.CommandType = CommandType.Refresh;

            ExecutionException executionException = new ExecutionException("Could not refresh page", null, command);

            using (Stream memory = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memory, executionException);
                memory.Position = 0; // Reset stream position
                executionException = (ExecutionException)formatter.Deserialize(memory);
            }

            Assert.IsNotNull(executionException, "ExecutionException object was not serialized properly.");
            Assert.IsNotNull(executionException.Command, "Command object was not serialized properly.");
            Assert.AreEqual(command.Arguments.Count, executionException.Command.Arguments.Count);
            Assert.AreEqual(command.Arguments[0], executionException.Command.Arguments[0]);
            Assert.AreEqual(command.Id, executionException.Command.Id);
            Assert.AreEqual(command.CommandType, executionException.Command.CommandType);
        }
    }
}